package com.graduate.framework.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduate.framework.annotation.CommonAuth;
import com.graduate.framework.annotation.NoAuth;
import com.graduate.framework.common.Result;
import com.graduate.framework.context.UserContext;
import com.graduate.framework.entity.Notifications;
import com.graduate.framework.entity.User;
import com.graduate.framework.service.NotificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notifications")
public class NotificationsController
{

    @Autowired
    private NotificationsService notificationsService;

    /**
     * 新增/修改
     *
     * @param notifications
     * @return
     */
    @PostMapping("/save")
    @CommonAuth
    public Result save(@RequestBody Notifications notifications)
    {
        boolean b = notificationsService.saveOrUpdate(notifications);
        if (b)
        {
            return Result.success();
        } else
        {
            return Result.error();
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @CommonAuth
    public Result deleteById(@PathVariable Integer id)
    {
        boolean b = notificationsService.removeById(id);
        if (b)
        {
            return Result.success();
        } else
        {
            return Result.error();
        }
    }

    /**
     * 根据id批量删除
     *
     * @param idList
     * @return
     */
    @PostMapping("/deleteBatch")
    @CommonAuth
    public Result deleteBatch(@RequestBody List<Integer> idList)
    {
        boolean b = notificationsService.removeByIds(idList);
        if (b)
        {
            return Result.success();
        } else
        {
            return Result.error();
        }
    }

    // 获取用户最新未读消息、未读消息数量
    @GetMapping("/findNewNotice")
    @CommonAuth
    public Result findNewNoticeList()
    {
        JSONObject resJson = new JSONObject();
        List<Notifications> notifications = notificationsService.lambdaQuery()
                .eq(Notifications::getIsRead, false)
                .eq(Notifications::getReceiverId, UserContext.getUser().getId())
                .orderByDesc(Notifications::getSendTime).list();
        Notifications one = notifications.stream().findFirst().orElse(null);
        Long count = notificationsService.lambdaQuery()
                .eq(Notifications::getIsRead, false)
                .eq(Notifications::getReceiverId, UserContext.getUser().getId())
                .count();
        resJson.set("newNotice", one);
        resJson.set("newNoticeCount", count);
        return Result.success(resJson);
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping("/findAll")
    @CommonAuth
    public Result findAll()
    {
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        return Result.success(notificationsService.list(queryWrapper));
    }

    /**
     * 分页查询
     *
     * @param pageNum：页码
     * @param pageSize：每页条数
     * @return
     */
    @GetMapping("/findPage")
    @CommonAuth
    @NoAuth
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(name = "content", defaultValue = "") String content,
                           @RequestParam(name = "receiverId", defaultValue = "") Integer receiverId
    )
    {
        Page<Notifications> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(content))
        {
            queryWrapper.like("content", content);
        }
        if (receiverId != null)
        {
            queryWrapper.eq("receiver_id", receiverId);
        }
        Page<Notifications> notificationsPage = notificationsService.page(page, queryWrapper);
        return Result.success(notificationsPage);
    }



    @GetMapping("/findByReceiverId")
    @CommonAuth
    public Result findByUserId(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(name = "content", defaultValue = "") String content
    )
    {
        User user = UserContext.getUser();
        Page<Notifications> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", user.getId());
        if (StrUtil.isNotBlank(content))
        {
            queryWrapper.like("content", content);
        }
        Page<Notifications> notificationsPage = notificationsService.page(page, queryWrapper);
        return Result.success(notificationsPage);
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @NoAuth
    public Result findById(@PathVariable Integer id)
    {
        Notifications notifications = notificationsService.getById(id);
        if (notifications != null)
        {
            return Result.success(notifications);
        } else
        {
            return Result.error();
        }
    }
}
